<template>
  <view>
    <!-- <input type="file" @change="selectFile" /> -->
    <button @click="selectImg">上传文件</button>
  </view>
</template>

<script>
	// import { Minio } from "minio-js";
	import { getUploadUrl } from '@/api/task.js'
	export default {
		data() {
			return {
				selectedFile: ''
			}
		},
		methods: {
			selectFile(e) {
				console.log('e', e)
			},
			async selectImg() {
				const baseUrl = 'https://oss.jinbitai.com/test1/s321.jpg?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=iqwgMU7FvMU7MbFzZKwD%2F20240805%2Fguiyang-02%2Fs3%2Faws4_request&X-Amz-Date=20240805T080300Z&X-Amz-SignedHeaders=host&X-Amz-Expires=7200&X-Amz-Signature=cf4d0549fde1091f8ba749100309ee1042615e4b9ac4fef7784c4af09aeab4fd'
				// let data = {
				// 	filename: name,
				// 	// product_code: code
				// 	product_code: code
				// }
				// const uploadRes = await getUploadUrl(data);
				// const { url, token, taskid } = uploadRes;
				
				uni.chooseImage({
					count: 1,
					success: (res) => {
						console.log('成功res', res)
						const tempFilePaths = res.tempFilePaths;
						const filePath = tempFilePaths[0]; // 选择第一张图片
						const fileName = tempFilePaths[0].match(/(.*\/)*(.*)$/)[2];
						uni.request({
							url: filePath, //uni.chooseImage返回的文件地址
							method: 'GET',
							responseType: 'arraybuffer', //此处注意不能少
							success: (res) => {
								console.log("字节流上传返回", res.data);
								let binaryData = res.data //返回的字节流
								// minio直传
								uni.request({
									url: baseUrl,//后端返回的签名地址
									data: binaryData , //上传的文件字节流
									method: 'PUT',
									header: {
										'Content-Type': 'application/octet-stream', //此处注意
										'Content-Disposition':'inline'
									},
									success: (res) => {
										console.log("直传返回", res);
									}
								});
							}
						});
						
						// const minioClient = new Minio.Client({
						// 	endPoint: 'oss.jinbitai.com', // minio的ip，直接替换自己的即可
						// 	// cors: true,
						// 	// port: 7009,
						// 	useSSL: true, // 是否使用ssl
						// 	accessKey: 'iqwgMU7FvMU7MbFzZKwD', // 登录的accessKey
						// 	secretKey: 'Cq74nRLbSiBvItSM4Z5JyWLgw40gRMQHWJLXgFq2',
						// 	region: 'guiyang-02'
						// });
						
						// console.log('minioClient', minioClient)
						
						// minioClient.putObject('test', 'file.png', filePath, function (err, objInfo) {
						// 	if (err) {
						// 		console.log('err', err)
						// 		return console.log(err) // err should be null
						// 	}
						// 	console.log('Success', objInfo)
						// });
						
						
					}
				});
			},
		}
	}
</script>

<style>
</style>